<?php

if (!isset($_SESSION))
    session_start();
require('../../Smarty/libs/Smarty.class.php');

$smarty = new Smarty;

$smarty->setTemplateDir('./smarty/templates/');
$smarty->setCompileDir('./smarty/templates_c/');
$smarty->setConfigDir('./smarty/configs/');
$smarty->setCacheDir('./smarty/cache/');

$status = $_SESSION['logged_bool'];

$smarty->assign("logged", $status);


/*
 * Copy session contents to the _POST superglobal IF the currentPage
 * Session variable is set
 */
if ((!empty($_SESSION['currentPage'])) && ($_SESSION['currentPage'] == 'bookingPage.php')) {
    $_POST = $_SESSION['login_guard']['postcontents'];
}

if ((!empty($_SESSION['book_guard']['postcontents']['currentPage'])) && ($_SESSION['book_guard']['postcontents']['currentPage'] == 'bookingPage.php')) {
    $_POST = $_SESSION['book_guard']['postcontents'];
    unset($_SESSION['book_guard']);
}


$contactinfo = array();
$roomprices = array();

if (isset($_POST)) {
    if (isset($_POST["step"])) {
        $stepOneCompleted = (boolean) $_POST["step"];
    } else {
        $stepOneCompleted = false;
    }
}
if (isset($_SESSION["roomsNumber"])) {
    $guestsCount = (int) $_SESSION["roomsNumber"];
} else {
    $guestsCount = 0;
}

$guestsinfo = array();
for ($i = 1; $i <= $guestsCount; $i++) {
    $guestsinfo[$i]["firstname"] = isset($_POST["guest_firstName$i"]) ? $_POST["guest_firstName$i"] : "";
    $guestsinfo[$i]["lastname"] = isset($_POST["guest_lastName$i"]) ? $_POST["guest_lastName$i"] : "";
    $guestsinfo[$i]["smokingPreference"] = isset($_SESSION["room$i" . "_smokingPreference"]) ? $_SESSION["room$i" . "_smokingPreference"] : "";
    $guestsinfo[$i]["adults"] = isset($_SESSION["room$i" . "_adults"]) ? $_SESSION["room$i" . "_adults"] : "";
    $guestsinfo[$i]["children"] = isset($_SESSION["room$i" . "_children"]) ? $_SESSION["room$i" . "_children"] : "";
}

if ($stepOneCompleted) { // true
    $hotelinfo = $_SESSION["hotelinfo"];
    $contactinfo = $_SESSION["book_step1_contactinfo"] = $contactinfo;
    $guestsinfo = $_SESSION["book_step1_guestsinfo"];
    $hotelinfo["email"] = $_POST["contactDetails_email"];
    $hotelinfo["confirmEmail"] = $_POST["contactDetails_confirmEmail"];
    $hotelinfo["phone"] = $_POST["contactDetails_phoneNumber"];

    for ($i = 1; $i <= $guestsCount; $i++) {
        $hotelinfo["guests"][$i]["firstname"] = isset($_POST["guest_firstName$i"]) ? $_POST["guest_firstName$i"] : "";
        $hotelinfo["guests"][$i]["lastname"] = isset($_POST["guest_lastName$i"]) ? $_POST["guest_lastName$i"] : "";
        $hotelinfo["guests"][$i]["smokingPreference"] = isset($_SESSION["room$i" . "_smokingPreference"]) ? $_SESSION["room$i" . "_smokingPreference"] : "";
    }
} else {    // false
    /*
     * Getting post contents 
     */

    $hotelThumbnail = isset($_POST["hotelImage"]) ? $_POST["hotelImage"] : "";
    $hotelName = isset($_POST["hotelName"]) ? $_POST["hotelName"] : "";
    $checkin = isset($_POST["checkin"]) ? $_POST["checkin"] : "";
    $checkout = isset($_POST["checkout"]) ? $_POST["checkout"] : "";
    $roomDescription = isset($_POST["roomDescription"]) ? $_POST["roomDescription"] : "";
    $smokingPreferences = isset($_POST["smokingPreferences"]) ? $_POST["smokingPreferences"] : "";
    $total = isset($_POST["total"]) ? $_POST["total"] : "";
    $surchargeTotal = isset($_POST["surchargeTotal"]) ? $_POST["surchargeTotal"] : "";
    $currencyCode = isset($_POST["currencyCode"]) ? $_POST["currencyCode"] : "";




    if (isset($_POST["contactDetails_email"])) {
        $contactinfo["email"] = $_POST["contactDetails_email"];
    } else {
        $contactinfo["email"] = "";
    }
    if (isset($_POST["contactDetails_confirmEmail"])) {
        $contactinfo["confirmEmail"] = $_POST["contactDetails_confirmEmail"];
    } else {
        $contactinfo["confirmEmail"] = "";
    }
    if (isset($_POST["contactDetails_phoneNumber"])) {
        $contactinfo["phone"] = $_POST["contactDetails_phoneNumber"];
    } else {
        $contactinfo["phone"] = "";
    }


    for ($i = 1; $i <= $guestsCount; $i++) {
        $roomprices[$i]["totalWithoutTax"] = isset($_POST["room$i" . "priceNoTax"]) ? $_POST["room$i" . "priceNoTax"] : "";
    }


    if (!isset($_SESSION["hotelinfo"])) {
        $hotelinfo = array(
            "hotelid" => $_SESSION["roomAvail"]["id"],
            "supplierType" => $_SESSION["roomAvail"]["supplierType"],
            "roomRateKey" => $_POST["roomRateKey"],
            "roomRateCode" => $_POST["roomRateCode"],
            "roomTypeCode" => $_POST["roomCode"],
            "averageRate" => $_POST["averageRate"],
            "nightlyRateTotal" => $_POST["nightlyRateTotal"],
            "image" => $hotelThumbnail,
            "name" => $hotelName,
            "checkin" => $checkin,
            "checkout" => $checkout,
            "roomDescription" => $roomDescription,
            "smokingPreference" => $smokingPreferences,
            "roomprices" => $roomprices,
            "total" => $total,
            "taxes" => $surchargeTotal,
            "currencyCode" => $currencyCode,
            "cancelationPolicy" => $_SESSION["cancellationPolicy"]
        );
        $_SESSION["hotelinfo"] = $hotelinfo;
    } else {
        $hotelinfo = $_SESSION["hotelinfo"];

        $_SESSION["book_step1_contactinfo"] = $contactinfo;
        $_SESSION["book_step1_guestsinfo"] = $guestsinfo;
    }
}
$bookData = array(
    "rooms" => $guestsinfo,
    "email" => "",
    "confemail" => "",
    "phone" => ""
);



$country = array(
    1 => array(
        "name" => "America",
        "hasStates" => true,
        "states" => array(
            1 => array(
                "code" => "HA",
                "name" => "Hawaii"
            )
        ),
        "code" => "SUA"
    ),
    2 => array(
        "name" => "Romania",
        "hasStates" => false,
        "states" => array(),
        "code" => "RO"
    ),
    3 => array(
        "name" => "Test",
        "hasStates" => true,
        "states" => array(
            1 => array(
                "code" => "HAX",
                "name" => "Hawaiix"
            )
        ),
        "code" => "SUA"
    )
);


$bookData2 = array(
    "creditcard" => array(
        "type" => array(
            "Visa" => "VS",
            "MasterCard" => "CA"
        ),
    ),
    "billinginfo" => array(
        "countries" => "",
        "city" => "",
        "address" => "",
        "postal" => "",
        "state" => ""
    ),
    "booking" => array(
        "cancelPolicy" => ""
    ),
    "countries" => $country
);

$smarty->display("preHeader.tpl");
$smarty->assign("logged", "");
$smarty->display("header.tpl");

if ($stepOneCompleted) {
    $smarty->assign("book", $bookData2);
    $smarty->assign("book_file", "booking_step_two.tpl");
} else {
    $smarty->assign("book", $bookData);
    $smarty->assign("book_file", "booking_step_one.tpl");
}

$smarty->assign("hotelinfo", $hotelinfo);
$smarty->display("booking_page.tpl");
?>